﻿var Normal_VoteList;
var Normal_VoteStore;
var Normal_VoteId
// 功能模块grid展示
Normal_VoteList = function() {
    var url = "/manager/Normal_Vote/List";
    var sm = Ext.create('Ext.selection.CheckboxModel');

    //grid 填充
    Ext.define('Normal_VoteGridModel', {
        extend: 'Ext.data.Model',
        fields: [
            { name: "Id", type:"string"},
            { name: "Title", type:"string"},
            { name: "VoteText", type:"string"},
            { name: "Type", type:"string"},
            { name: "Enabled", type:"string"},
            { name: "Description", type:"string"},
            { name: "AllowGuest", type:"string"},
            { name: "AddDate", type:"datetime"},
            { name: "EndDate", type:"datetime"},
            { name: "StartDate", type:"datetime"},
            { name: "AllowView", type:"string"},
            { name: "Interval", type:"string"},
            { name: "TemplateId", type:"string"},
            { name: "UserId", type:"string"}
            ]
    });

    //grid获取 json数组
    Normal_VoteStore = Ext.create('Ext.data.Store', {
        pageSize: CMN.pageSize,
        model: 'Normal_VoteGridModel',
        buffered: false,  //是否动态加载
        proxy: {
            actionMethods: 'post',
            type: 'ajax',
            url: url,
            reader: {
                type: 'json',
                root: 'root',
                totalProperty: 'totalPorperty'
            }
        },
        sorters: [{
            property: 'Id',
            direction: 'DESC'
}]
        });

        //分页设置
        var pagingBar = Ext.create('Ext.PagingToolbar', {
            displayInfo: true, emptyMsg: "没有数据显示", displayMsg: "显示从{0}条数据到{1}条数据，共{2}条数据",
            store: Normal_VoteStore,
            pageSize: CMN.pageSize,
            //显示右下角信息
            prevText: "上一页",
            nextText: "下一页",
            refreshText: "刷新",
            lastText: "最后页",
            firstText: "第一页",
            beforePageText: "当前页",
            afterPageText: "共{0}页"
        });

        //删除选中行信息
       var DeleteNormal_VoteFn = function() {
            var row = Ext.getCmp("Normal_VotePanel").getSelectionModel().getSelection();
            if (row.length == 0) {
                Ext.Msg.alert("提示信息", "请您至少选择一个!");
            }
            else if (row.length > 1) {
                var idList = "";
                for (var i = 0; i < row.length; i++) {
                    if (i == 0) {
                        idList = row[0].data.Id;
                    }
                    else {
                        idList = idList +"_" + row[i].data.Id;
                    }
                }
                //Ext.Msg.alert("提示信息", "对不起只能选择一个!");
                Ext.Msg.confirm("提 示!", "您确定要删除" + idList + "吗?", function(btn) {
                    if (btn == "yes") {
                        Ext.Ajax.request({
                            url: "/manager/Normal_Vote/DeleteList/" + idList,
                            method: "get",
                            success: function(d) {
                                //成功后
                                var action = Ext.JSON.decode(d.responseText);
                                //alert(action.success);
                                var flag = action.success;
                                if (flag == "true") {
                                    Normal_VoteStore.loadPage(1);
                                    Ext.MessageBox.alert("恭喜", "删除成功!");
                                }
                                else {
                                    var message = action.result.message;
                                    if (message) {
                                        Ext.MessageBox.alert("删除失败!", message);
                                    }
                                    else {
                                        Ext.MessageBox.alert("提示!", "删除失败!");
                                    }
                                }
                            },
                            failure: function(form, action) {
                                Ext.MessageBox.alert("提示!", "修改失败!");
                            }
                        })
                    }
                    else {

                    }
                })
            }
            else if (row.length == 1) {
                Ext.Msg.confirm("提 示!", "您确定要删除" + row[0].data.Id + "吗?", function(btn) {
                    if (btn == "yes") {
                        Ext.Ajax.request({
                            url: "/manager/Normal_Vote/Delete/" + row[0].data.Id,
                            method: "POST",
                            success: function(d) {
                                //成功后
                                var action = Ext.JSON.decode(d.responseText);
                                //alert(action.success);
                                var flag = action.success;
                                if (flag == "true") {
                                    Normal_VoteStore.loadPage(1);
                                    Ext.MessageBox.alert("恭喜", "删除成功!");
                                }
                                else {
                                    var message = action.result.message;
                                    if (message) {
                                        Ext.MessageBox.alert("删除失败!", message);
                                    }
                                    else {
                                        Ext.MessageBox.alert("提示!", "删除失败!");
                                    }
                                }
                            },
                            failure: function(form, action) {
                                Ext.MessageBox.alert("提示!", "修改失败!");
                            }
                        })
                    }
                    else {

                    }
                })
            }
        }


        //修改选中行信息
       var EditNormal_VoteFn = function() {
            var row = Ext.getCmp("Normal_VotePanel").getSelectionModel().getSelection();
            if (row.length == 0) {
                Ext.Msg.alert("提示信息", "您没有选中任何行!");
            }
            else if (row.length > 1) {
                Ext.Msg.alert("提示信息", "对不起只能选择一个!");
            } else if (row.length == 1) {
                EditNormal_Vote(row[0].data.Id);
            }

        }

        //新增信息
       var SaveNormal_VoteFn = function() {
            InsertNormal_Vote();
        }
        ShowNormal_Vote_RecordWin = function(extId) {
        Normal_VoteId = extId;
           var Normal_Vote_RecordWin = new Ext.Window({
                title: '投票选项管理',
                width: 600,
                height: 450,
                plain: true,
                layout: "fit",
                resizable: false,
                defaultType: "textfield",
                collapsible: true, //允许缩放条
                closeAction: 'destroy',
                closable: true,
                //弹出模态窗体
                modal: 'true',
                buttonAlign: "center",
                bodyStyle: "padding:5px",
                loader: {
                url: '/Manager/Normal_Vote_Record/Index/' + Normal_VoteId,
                    contentType: 'html',
                    autoLoad: true,
                    scripts: true,
                    scope: this,
                    loadMask: true
                }
            });

            Normal_Vote_RecordWin.show();
        }
        //grid配置
        var grid = Ext.create('Ext.grid.Panel', {
            id: "Normal_VotePanel",
            renderTo: "Normal_VoteGrid",
            layout: "fit",
            alias: 'widget.methodPanel',
            columnLines: true,
            loadMask: true,
            frame: true,
            border: false,
            selModel: sm,   //选择框
            autoScroll: true,
            store: Normal_VoteStore,
            ds: Normal_VoteStore,
            sm: sm,
            columnLines: true,
            //cm: cm2,
            viewConfig: {
                stripeRows: true,
                trackOver: true,
                emptyText: '<div style="color:#999;margin:5px;">当前没有记录显示<div>'
            },
            stripeRows: true,
            listeners: {
                "itemdblclick": function(g, rowIndex, e) {
                    EditNormal_VoteFn();
                }
            },
            bbar: pagingBar,
            tbar: [            		 //工具条设置
            		 {
            		 text: "新增",
            		 tooltip: "新增",
            		 iconCls: "addicon",
            		 handler: SaveNormal_VoteFn
           }, "", "-", "", {
               text: "编辑",
               tooltip: "编辑",
               iconCls: "editicon",
               handler: EditNormal_VoteFn
           }, "", "-", "", {
               text: "删除",
               tooltip: "删除",
               iconCls: "deleteicon",
               handler: DeleteNormal_VoteFn
           }, "-"],
            columns: [
               Ext.create('Ext.grid.RowNumberer'),
                { header: "递增Id", dataIndex: "Id", width: 80, sortable: true },
                { header: "标题", dataIndex: "Title", width: 80, sortable: true },
                { header: "类型 多选 单选", dataIndex: "Type", width: 40, renderer: function(value) { if (value == "1") { return "是"; } else { return "否"; } }, sortable: true },
                { header: "是否启用", dataIndex: "Enabled", width: 40, renderer: function(value) { if (value == "1") { return "是"; } else { return "否"; } }, sortable: true },
                { header: "添加时间", dataIndex: "AddDate", width: 80, sortable: true },
                { header: "结束时间", dataIndex: "EndDate", width: 80, sortable: true },
                { header: "开始时间", dataIndex: "StartDate", width: 80, sortable: true },
                { header: "允许查看投票结果", dataIndex: "AllowView", width: 1, renderer: function(value) { if (value == "1") { return "是"; } else { return "否"; } }, sortable: true },
                { header: "", dataIndex: "UserId", width: 80, sortable: true },
                { header: "选项管理", dataIndex: "Id", renderer: function(value) { return "<a href='javascript:void(0);' title='投票选项管理' onclick='ShowNormal_Vote_RecordWin(" + value + ")'>投票选项管理</a>" }, width: 80 }, 
                { header: "删除", dataIndex: "Id", renderer: function(value) { return "<a href='javascript:void(0);' title='删除' onclick='DeleteNormal_VoteFn()'class='deleteBtn' >&nbsp;&nbsp;&nbsp;</a>" }, width: 40 }    
            ]
        });
        Normal_VoteStore.loadPage(1);
    }
    Normal_VoteList();
